package com.mcptt.config.user.profile.sub;

import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.user.profile.sub.sub.ImplicitAffiliations;
import com.mcptt.config.user.profile.sub.sub.McGroupInfo;
import com.mcptt.config.user.profile.sub.sub.PrivateEmergencyAlert;

public class OnNetwork
{
	public static final String Name = "OnNetwork";
	private static final String Name_Index = "index";
	private static final String Name_MaxAffiliationsN2 = "MaxAffiliationsN2";
	private static final String Name_MaxSimultaneousTransmissionsN7 = "MaxSimultaneousTransmissionsN7";
	
	private String index = "";
	private McGroupInfo mcGroupInfo = null;
	private int maxAffiliationsN2 = 0;
	private ImplicitAffiliations implicitAffiliations = null;
	private int maxSimultaneousTransmissionsN7 = 0;
	private PrivateEmergencyAlert privateEmergencyAlert = null;

	public void parse(int mcType,Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		index = elem.getAttributeValue(Name_Index);
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);
			
			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			
			if(name.equals(McGroupInfo.Name_Mcptt)
					||name.equals(McGroupInfo.Name_Mcvideo)
					|| name.equals(McGroupInfo.Name_Mcdata))
			{
				mcGroupInfo = new McGroupInfo();
				mcGroupInfo.parse(mcType,subElem);
			}
			else if(name.equals(Name_MaxAffiliationsN2))
			{
				String value = subElem.getText();
				maxAffiliationsN2 = CheckData.parseInt(value);
			}
			else if(name.equals(ImplicitAffiliations.Name))
			{
				implicitAffiliations = new ImplicitAffiliations();
				implicitAffiliations.parse(subElem);
			}
			else if(name.equals(Name_MaxSimultaneousTransmissionsN7))
			{
				String value = subElem.getText();
				maxSimultaneousTransmissionsN7 = CheckData.parseInt(value);
			}
			else if(name.equals(PrivateEmergencyAlert.Name))
			{
				privateEmergencyAlert = new PrivateEmergencyAlert();
				privateEmergencyAlert.parse(subElem);
			}
		}  
	}
	
	public McGroupInfo getMcGroupInfo()
	{
	    return mcGroupInfo;
	}
	
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Name).append(" ").append(Name_Index).append("=\"").append(index).append("\">").append("\r\n");
		
		strBuf.append(mcGroupInfo);
		
		strBuf.append("    ").append("<").append(Name_MaxAffiliationsN2).append(">");
		strBuf.append(maxAffiliationsN2);
		strBuf.append("</").append(Name_MaxAffiliationsN2).append(">").append("\r\n");
		
		strBuf.append(implicitAffiliations);
		
		strBuf.append("    ").append("<").append(Name_MaxSimultaneousTransmissionsN7).append(">");
		strBuf.append(maxSimultaneousTransmissionsN7);
		strBuf.append("</").append(Name_MaxSimultaneousTransmissionsN7).append(">").append("\r\n");
		
		strBuf.append(privateEmergencyAlert);
		
		strBuf.append("</").append(Name).append(">").append("\r\n");

		return strBuf.toString();
	}
}
